﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Xml;

namespace HtmlCaches
{
    public class StringMd5
    {
        public StringMd5()
        { }

        /// <summary>
        /// md5转换
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToMd5(string s)
        {
            MD5CryptoServiceProvider m = new MD5CryptoServiceProvider();
            byte[] btstr = Encoding.UTF8.GetBytes(s);
            byte[] btmd5 = m.ComputeHash(btstr);
            StringBuilder builder = new StringBuilder();
            foreach (byte item in btmd5)
            {
                builder.Append(item.ToString("x2"));
            }

            return builder.ToString();
        }

        /// <summary>
        /// 取到htmlcaches.config中的配置参数
        /// </summary>
        /// <param name="updatetimediff">更新时间差</param>
        /// <param name="htmlcache">cache开关锁</param>
        /// <param name="savepath">保存路径</param>
        /// <param name="filterpage">页面过滤（哪些页面才生成）</param>
        public static void GetParameters(out string updatetimediff, out string htmlcache, out string savepath, out string filterpage, out string listpage)
        {

            string htmlcachesFile = HttpContext.Current.Server.MapPath("/htmlcaches.config");
            XmlDocument htmlcachesconfig = new XmlDocument();
            htmlcachesconfig.Load(htmlcachesFile);
            XmlNode root = htmlcachesconfig.SelectSingleNode("htmlcaches");

            string _updatetimediff = string.Empty;
            string _htmlcache = string.Empty;
            string _savepath = string.Empty;
            string _filterpage = string.Empty;
            string _listpage = string.Empty;
            foreach (XmlNode n in root.ChildNodes)
            {
                if (n.Name == "updatetimediff")
                {
                    _updatetimediff = n.InnerText;
                    continue;
                }
                if (n.Name == "htmlcache")
                {
                    _htmlcache = n.InnerText;
                    continue;
                }
                if (n.Name == "savepath")
                {
                    _savepath = n.InnerText;
                    continue;
                }
                if (n.Name == "filterpage")
                {
                    _filterpage = n.InnerText;
                    continue;
                }
                if (n.Name == "listpage")
                {
                    _listpage = n.InnerText;
                    continue;
                }


            }

            updatetimediff = _updatetimediff;
            htmlcache = _htmlcache;
            savepath = _savepath;
            filterpage = _filterpage;
            listpage = _listpage;
        }
    }
}
